package sunyu.controller.shiro;

import org.slf4j.Logger;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import sunyu.controller.CommonController;
import sunyu.pojo.ShiroResource;
import sunyu.pojo.easyui.Result;
import sunyu.pojo.easyui.TreeNode;
import sunyu.service.ResourceService;
import sunyu.toolkit.core.LogKit;
import sunyu.toolkit.core.MapKit;
import sunyu.toolkit.core.RandomKit;
import sunyu.toolkit.core.StringKit;

import javax.annotation.Resource;
import java.util.*;

/**
 * 菜单控制器
 *
 * @author 孙宇
 */
@Controller
@RequestMapping("/shiro/resource")
public class ResourceController extends CommonController {

    private static final Logger logger = LogKit.getLogger();

    @Resource(name = "resourceService")
    ResourceService resourceService;


    @RequestMapping("/tree")
    @ResponseBody
    public List<TreeNode> tree() {
        return getTreeNodes(null);
    }

    @RequestMapping("/menu")
    @ResponseBody
    public List<TreeNode> menu() {
        Map params = new HashMap();
        params.put("type", 0);//只查询菜单

        return getTreeNodes(params);
    }

    private List<TreeNode> getTreeNodes(Map params) {
        List<TreeNode> nodes = new ArrayList<>();
        List<ShiroResource> l = resourceService.select(params);
        sort(l);
        for (ShiroResource m : l) {
            TreeNode n = new TreeNode();
            n.setId(m.getId());
            n.setPid(m.getPid());
            n.setText(m.getName());
            n.setAttributes(MapKit.convert(m));
            nodes.add(n);
        }
        return nodes;
    }

    private void sort(List<ShiroResource> list) {
        Collections.sort(list, (o1, o2) -> {
            if (o1.getSort() == null || o2.getSort() == null) {
                return 0;
            }
            if (o1.getSort().intValue() == o2.getSort().intValue()) {
                return 0;
            }
            return o1.getSort().intValue() > o2.getSort().intValue() ? 1 : -1;
        });
    }

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public String treeGridPage() {
        return "/template/shiro/resource/list";
    }

    @RequestMapping(value = "/list", method = RequestMethod.POST)
    @ResponseBody
    public List<ShiroResource> treeGrid() {
        List<ShiroResource> l = resourceService.select(null);
        sort(l);
        return l;
    }

    @RequestMapping(value = "/form", method = RequestMethod.GET)
    public String formPage(Model model, @RequestParam(required = false) String id, @RequestParam(required = false) String pid) {
        if (StringKit.isNotBlank(pid)) {
            ShiroResource resource = new ShiroResource();
            resource.setPid(pid);
            model.addAttribute("data", resource);
        }
        if (StringKit.isNotBlank(id)) {
            model.addAttribute("data", resourceService.selectByPrimaryKey(id));
        }
        return "/template/shiro/resource/form";
    }

    @RequestMapping(value = "/add", method = RequestMethod.POST)
    @ResponseBody
    public Result add(@ModelAttribute("data") ShiroResource record) {
        record.setId(RandomKit.UU64());
        Result r = new Result();
        resourceService.insertSelective(record);
        r.setSuccess(true);
        r.setMsg("添加成功！");
        return r;
    }

    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    @ResponseBody
    public Result edit(@ModelAttribute("data") ShiroResource record) {
        Result r = new Result();
        if (StringKit.isNotBlank(record.getPid())) {
            if (record.getPid().equals(record.getId())) {
                r.setMsg("父节点设置错误，保存失败！");
                return r;
            }
        }
        resourceService.updateByPrimaryKey(record);
        r.setSuccess(true);
        r.setMsg("编辑成功！");
        return r;
    }

    @RequestMapping("/remove/{id}")
    @ResponseBody
    public Result remove(@PathVariable("id") String id) {
        resourceService.deleteByPrimaryKey(id);
        Result r = new Result();
        r.setSuccess(true);
        r.setMsg("删除成功！");
        return r;
    }

}
